/**
 * 提前还款数据模型
 */
package com.p2p.front.service.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.p2p.base.constents.InnerAccountConsts;
import com.p2p.base.model.Money;
import com.p2p.front.dal.model.LoanInfo;
import com.p2p.front.dal.model.LoanRepay;
import com.p2p.front.dal.model.Tender;
import com.p2p.front.dal.model.TenderReturn;
import com.p2p.trans.client.enums.SubTransCodeEnum;
import com.p2p.trans.client.enums.TransCodeEnum;
import com.p2p.trans.client.model.TransInfo;

/**
 * @author ruiqi
 *
 */
public class BizReturnAdvanceModel {

	/**
	 * 标id
	 */
//	private final Long loanId;
	
	private final LoanInfo loanInfo;
	/**
	 * 所有还款
	 */
	private final List<LoanRepay> loanRepayList;
	
	/**
	 * 所有回款
	 */
	private final List<TenderReturn> tenderReturnList;
	
	//按投标id为key的map，投标信息里有用户的accountNo等信息
	private final Map<Long,Tender> tenderMap;
	
	//按投标id分别累加单笔投资剩余未还的本金
	private Map<Long,Money> allReturnPrincipalMap;
	
	//计算提前还款的总金额
	
	private Money allRepayPrincipal= new Money();
	
	//计算投资人还款的总额
	private Money allReturnPrincipal = new Money();

	public BizReturnAdvanceModel(LoanInfo loanInfo,List<LoanRepay> loanRepayList,List<TenderReturn> tenderReturnList,Map<Long,Tender> tenderMap) {
		//this.loanId = loanId;
		this.loanInfo = loanInfo;
		this.loanRepayList = loanRepayList;
		this.tenderReturnList = tenderReturnList;
		this.tenderMap = tenderMap;
	}
	
	public void init(){
		calculateAllRepayPrincipal();
		buildAllReturnPrincipalMap();
	}
	/**
	 * 根据借款人还款流水计算提前还款总金额
	 */
	public void calculateAllRepayPrincipal(){
		allRepayPrincipal = new Money();
		for(LoanRepay loanRepay:loanRepayList){
			Money tempPrincipal = loanRepay.getPrincipal();
			allRepayPrincipal.add(tempPrincipal);
		}
	}
	
	/**
	 * 根据投资人回款计算提前还款总金额
	 */
	public void buildAllReturnPrincipalMap(){
		allReturnPrincipal = new Money();
		allReturnPrincipalMap = new HashMap<Long,Money>();
		for(TenderReturn tenderReturn:tenderReturnList){
			
			Money tempMoney= allReturnPrincipalMap.get(tenderReturn.getTid());
			if(tempMoney==null){
				tempMoney = new Money();
				allReturnPrincipalMap.put(tenderReturn.getTid(), tempMoney);
			}
			tempMoney.add(tenderReturn.getPrincipal());
			allReturnPrincipal.add(tenderReturn.getPrincipal());
		}
	}
	
	/**
	 * 校验一致性
	 * @return
	 */
	public boolean checkPass(){
		
		if(loanRepayList.size()*tenderMap.size()!=tenderReturnList.size()){
			return false;
		}
		/*
		if(!allRepayPrincipal.equals(allReturnPrincipal)){
			return false;
		}*/
		int maxNo = loanInfo.getDeadline();
		int allAdvanceNo = loanRepayList.size();
		int tenderTimes = tenderMap.size();
		//计算误差率，由于有小数取整，所以会产生类似四舍五入的场景导致金额不一致
		long deviation = new Integer(tenderTimes*maxNo/allAdvanceNo).longValue();

		if(Math.abs(allRepayPrincipal.getCent()-allReturnPrincipal.getCent())>=deviation){
			return false;
		}
		return true;
	}
	
	/**
	 * 获取需要回款的批次转账list
	 * @return
	 */
	public List<TransInfo> getTenderReturnTransInfoList(){
		List<TransInfo> transInfoList = new ArrayList<TransInfo>(); 
		for(Tender tender:this.tenderMap.values()){
			Money returnAmount = allReturnPrincipalMap.get(tender.getTid());
			TransInfo transInfo = new TransInfo();
			transInfo.setTransAccountIn(tender.getUserAccount());
			transInfo.setTransAccountOut(InnerAccountConsts.innerMiddleAccountNo);
			transInfo.setTransCode(TransCodeEnum.TXCODE_TRANSFER);
			transInfo.setSubTransCode(SubTransCodeEnum.SUB_TRANSFER_RETURN);
			transInfo.setTransMemo("提前还款");
			transInfo.setTransAmount(returnAmount);
			transInfoList.add(transInfo);
		}
		return transInfoList;
	}
	
	public List<TransInfo> getReturnAdvanceFine(Money fine){
		List<TransInfo> transInfoList = new ArrayList<TransInfo>(); 
		double rate = 1.0*fine.getCent()/this.allReturnPrincipal.getCent();
		for(Tender tender:this.tenderMap.values()){
			Money returnAmount = allReturnPrincipalMap.get(tender.getTid());
			Money amount = new Money(new Double(returnAmount.getCent()*rate).longValue());
			TransInfo transInfo = new TransInfo();
			transInfo.setTransAccountIn(tender.getUserAccount());
			transInfo.setTransAccountOut(InnerAccountConsts.innerMiddleAccountNo);
			transInfo.setTransCode(TransCodeEnum.TXCODE_TRANSFER);
			transInfo.setSubTransCode(SubTransCodeEnum.SUB_TRANSFER_FINE);
			transInfo.setTransMemo("提前还款违约金");
			transInfo.setTransAmount(amount);
			if(amount.getCent()>0){
				transInfoList.add(transInfo);
			}
		}
		return transInfoList;
	}
	public List<LoanRepay> getLoanRepayList() {
		return loanRepayList;
	}

	
	public List<TenderReturn> getTenderReturnList() {
		return tenderReturnList;
	}


	public Map<Long, Tender> getTenderMap() {
		return tenderMap;
	}


	public Map<Long, Money> getAllReturnPrincipalMap() {
		return allReturnPrincipalMap;
	}

	public void setAllReturnPrincipalMap(Map<Long, Money> allReturnPrincipalMap) {
		this.allReturnPrincipalMap = allReturnPrincipalMap;
	}

	public Money getAllRepayPrincipal() {
		return allRepayPrincipal;
	}

	public void setAllRepayPrincipal(Money allRepayPrincipal) {
		this.allRepayPrincipal = allRepayPrincipal;
	}

	public Money getAllReturnPrincipal() {
		return allReturnPrincipal;
	}

	public void setAllReturnPrincipal(Money allReturnPrincipal) {
		this.allReturnPrincipal = allReturnPrincipal;
	}

	public LoanInfo getLoanInfo() {
		return loanInfo;
	}

	
	
	
	
	
}
